Ext.namespace("Milan.widget");
Milan.widget.FileComponent = Class.create();
Milan.widget.FileComponent.prototype = 
{
	/**
	 * 构造函数
	 * @param {Object} config
	 * config中以下属性
	 * config.container : 该组件所在的容器id
	 * config.targetCnt : 真实file控件所在的容器id(为文件域所在的form的formid);
	 * config.fileMaxCount : 控制一次能上传几个文件
	 * config.singleFile:true 或 false,为true时,永远只有一个文件,且fileMaxCount失效.为false时,可以多文件.
	 * config.onSelectedCallback : 选择完文件后的回调
	 * config.text:表示初始化时,要往container中写入的文字
	 * config.fileId:表示在选择文件后生成的fileinput的id,如不指定,则使用随机值,通常配合singleFile使用.
	 * config.fileName:表示在选择文件后生成的fileinput的name,如不指定,则使用随机值,通常配合singleFile使用.
	 *   
	 */
	initialize : function(config)
	{
		if(config.container == undefined) alert("config.container does not exist!");
		if(config.targetCnt == undefined) alert("config.targetCnt does not exist!");
		if(config.fileMaxCount == undefined) config.fileMaxCount = 1;
		if(config.width == undefined) config.width = "70px";
		this.config = config;
		this.trueFileUploadField = [];
		this.container = $(this.config.container);
		this.targetCnt = $(this.config.targetCnt);
		this.singleFile = this.config.singleFile || false;
		this.text = this.config.text || "上传附件";
		this.fileId = this.config.fileId ;
		this.fileName = this.config.fileName;
		this.createUI();
		this.fileCount = 0;
	},
	createUI : function()
	{
		var dd = "";
		if(this.fileCount>=this.config.fileMaxCount) dd = "disabled";
		var randKey  = this.getRandomKey();
		this.delegateFileUploadFieldKey = "fileUpload_" + randKey + "_id";
		
		var marginLeft = "";		
		if (Milan.navigator.isFF || Milan.navigator.isIE)
		{
			marginLeft = "margin-left:-12px;";
		}
		
		var marginTop = "";
		if (Milan.navigator.isIE && parseInt(Milan.navigator.version)<=7)
		{
			marginTop = "margin-top:-6px;";
		}
		
		var sWidth = "width:"+this.config.width+";";
		
		//var vName= "fileUpload_" + randKey + "_name";
		var vName = "attachFiles";
		var vId = "fileUpload_" + randKey + "_id";
		
		var fileCom = "<input type='file' class='hiddenFileField' " + dd + " size='1' style='z-index:1;"+sWidth+marginLeft+marginTop+"display:none' name='" +vName+ "' id='" +vId+ "'/>";
		var fileUploadLabel = this.text;
		this.container.innerHTML = fileCom + fileUploadLabel;
		this.delegateFileUploadField = $(vId);
		Event.observe(this.delegateFileUploadField,"change",this.onchangeCallBack.bind(this));
		this.delegateFileUploadField.style.cursor = "pointer";

		var invokeTime = 0;
		var fuckingIE = setInterval(function(){
			if(invokeTime<3)
			{
				this.delegateFileUploadField.style.position = "absolute";
				this.delegateFileUploadField.style.display = "inline";
			}
			else
			{
				clearInterval(fuckingIE);
			}
			invokeTime++;
		}.bind(this),100)

	},
	getRandomKey : function()
	{
		return Milan.commonutils.Randomizer.rand(100000);
	},
	onchangeCallBack : function(e)
	{
		var evnt =  e || window.event;
		var target = evnt.target || evnt.srcElement;
		if (target.tagName.toUpperCase() == "INPUT"&&target.type.toUpperCase() == "FILE") 
		{
			this.delegateFileUploadField.style.display = "none";
			if (this.singleFile)
			{
				if ($(this.targetCnt)) $(this.targetCnt).innerHTML="";
			}
			if (this.config.fileName)
				this.delegateFileUploadField.name =this.config.fileName;
			
			if (this.config.fileId)
				this.delegateFileUploadField.id =this.config.fileId;
				
			$(this.targetCnt).appendChild(this.delegateFileUploadField);
			this.config.onSelectedCallback(this.delegateFileUploadField.id);
			this.fileCount ++;
			this.createUI();
		}
	}
}


